package in.ac.dei.edrp.cms.daoimpl.adddropcourse;

import java.util.ArrayList;
import java.util.List;

import in.ac.dei.edrp.cms.dao.adddropcourse.AddDropCourseService;
import in.ac.dei.edrp.cms.domain.adddropcourse.AddDropCourseBean;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class AddDropCourseImpl extends SqlMapClientDaoSupport implements
		AddDropCourseService {
	
	private static Logger logObj = Logger.getLogger(AddDropCourseImpl.class);
	
	private TransactionTemplate transactionTemplate;

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}	

	@SuppressWarnings("unchecked")
	public List<AddDropCourseBean> getStudentCourseList(
			String regNumber) {
		List<AddDropCourseBean> addDropCourseBeans = new ArrayList<AddDropCourseBean>();
		try {
			addDropCourseBeans=getSqlMapClientTemplate().queryForList("addDropCourse.getStudentCourseList",regNumber);
		} catch (Exception e) {
			logObj.error(e.getMessage());
			System.out.println(e.getMessage());
		}
		return addDropCourseBeans;
	}

	@SuppressWarnings("unchecked")
	public List<AddDropCourseBean> getProgramDetails(String regNumber) {

		List<AddDropCourseBean> addDropCourseBeans = new ArrayList<AddDropCourseBean>();
		try {
			addDropCourseBeans=getSqlMapClientTemplate().queryForList("addDropCourse.getProgramDetails",regNumber);
		} catch (Exception e) {
			logObj.error(e.getMessage());
			System.out.println(e.getMessage());
		}
		return addDropCourseBeans;
	}

	@SuppressWarnings("unchecked")
	public List<AddDropCourseBean> getCourseAvailable(AddDropCourseBean addDropCourseBean) {
		List<AddDropCourseBean> addDropCourseBeans = new ArrayList<AddDropCourseBean>();
		try {
			addDropCourseBeans=getSqlMapClientTemplate().queryForList("addDropCourse.getCourseAvailable",addDropCourseBean);
		} catch (Exception e) {
			logObj.error(e.getMessage());
			System.out.println(e.getMessage());
		}
		return addDropCourseBeans;
	}

	public String addDropStudentCourse(final List<AddDropCourseBean> addCourseBeans,
			final AddDropCourseBean dropCourseBean) {
		return (String)transactionTemplate.execute(new TransactionCallback(){
				public String doInTransaction(TransactionStatus ts) {
					String result="";
					Object savepoint=new Object();
					try {
						savepoint=ts.createSavepoint();
						
						getSqlMapClientTemplate().delete("addDropCourse.deleteCourse",dropCourseBean);
						
						for(int i=0;i<addCourseBeans.size();i++)
						{
							getSqlMapClientTemplate().insert("addDropCourse.insertCourse",addCourseBeans.get(i));
						}
						
						result="true";
					} catch (Exception e) {
						ts.rollbackToSavepoint(savepoint);
						logObj.error(e.getMessage());
						System.out.println(e.getMessage());
						result="false";
					}
					return result;
				}
				
			});
	}

	@SuppressWarnings("unchecked")
	public String getTotalCredits(String regNumber) {
		List<AddDropCourseBean> addDropCourseBeans = new ArrayList<AddDropCourseBean>();
		try {
			addDropCourseBeans=getSqlMapClientTemplate().queryForList("addDropCourse.getTotalCredits",regNumber);
		} catch (Exception e) {
			logObj.error(e.getMessage());
			System.out.println(e.getMessage());
		}
		return addDropCourseBeans.get(0).getCredits();
	}

}
